//
//  HZJMusicManagerViewController.m
//  HZ_Beautiful_Music
//
//  Created by dllo on 16/4/6.
//  Copyright © 2016年 ui. All rights reserved.
//

#import "HZJMusicManagerViewController.h"
#import "HZJDownLoadViewController.h"
#import "HZJMusicManagerTVCell.h"
@interface HZJMusicManagerViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, strong) UITableView *tableView;

@property(nonatomic,strong)NSIndexPath*selectPath;
@property(nonatomic,strong)NSMutableArray *selectIndexPaths;

@property (nonatomic, assign) NSInteger num;
@property (nonatomic, assign) BOOL isOn;

@end

@implementation HZJMusicManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.isOn = NO;
    self.selectIndexPaths = [NSMutableArray arrayWithCapacity:0];
    [self creatTable];
    
//    UIBarButtonItem *itemOne = [[UIBarButtonItem alloc] initWithTitle:@"全 选 " style:UIBarButtonItemStylePlain target:self action:@selector(itemOneAction:)];
    UIBarButtonItem *itemTwo = [[UIBarButtonItem alloc] initWithTitle:@"  下 载  " style:UIBarButtonItemStylePlain target:self action:@selector(itemTwoAction:)];
    
    self.navigationItem.rightBarButtonItems = @[itemTwo];
    
    // Do any additional setup after loading the view.
}



#pragma mark - 下载
- (void)itemTwoAction:(UIBarButtonItem *)item {

    HZJDownLoadViewController *downVC = [[HZJDownLoadViewController alloc] init];
    downVC.data = self.selectIndexPaths; // 选择的
    downVC.dataSource = self.data;
    
//    
//    NSArray *array = @[self.selectIndexPaths, self.data];
//    NSNotificationCenter *centerArr = [NSNotificationCenter defaultCenter];
//    
//    [centerArr postNotificationName:@"ARRAY" object:array userInfo:nil];
    [self.navigationController pushViewController:downVC animated:YES];

}

- (void)creatTable {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerClass:[HZJMusicManagerTVCell class] forCellReuseIdentifier:@"HZJMusicManagerTVCell"];
    
    [self.view addSubview:_tableView];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HZJMusicManagerTVCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HZJMusicManagerTVCell"];
    
    cell.detailModel = self.data[indexPath.row];

//    ------------------------
//    if (!self.isOn) {
    
    if([self.selectIndexPaths containsObject:indexPath])//如果这个数组中有当前所点击的下标，那就标记为打钩
    {
        cell.accessoryType=UITableViewCellAccessoryCheckmark;
        
    }
    
    else
    {
        cell.accessoryType=UITableViewCellAccessoryNone;
    }
//    }

    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (!self.isOn) {
//        [self.selectIndexPaths removeAllObjects];
    if([self.selectIndexPaths containsObject:indexPath])//在NSMutableArray中用（bool）类型的containsObject判断这个对象是否存在这个数组中（判断的是内存地址）contains：包含
    {
        //存在以选中的，就执行（为真就执行）把存在的移除
        [self.selectIndexPaths removeObject:indexPath];//把这个cell的标记移除
    }
    else//不存在这个标记，那点击后就添加到这个数组中
    {
        [self.selectIndexPaths addObject:indexPath];
    }
    }
    [_tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationFade];//重新刷新这行

    [_tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    self.navigationItem.title = [NSString stringWithFormat:@"已选%ld项", _selectIndexPaths.count];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
